﻿using System.Xml.Linq;
using Engine;
using Game;


public class ShopWidget : CanvasWidget
{
    public ComponentPlayer m_componentPlayer;

    public SubsystemNK m_subsystemHL;

    public int? value;


    public ComponentShop m_componentShop;

    public ComponentMiner componentMiner;

    public ButtonWidget m_shopButton;
    public ButtonWidget m_shop1Button;
    public ButtonWidget m_shop2Button;
    public ButtonWidget m_shop3Button;
    public ButtonWidget m_shop4Button;
    public ButtonWidget m_shop5Button;
    public ButtonWidget m_shop6Button;
    public ButtonWidget m_shop7Button;
    public ButtonWidget m_shop8Button;
    public ButtonWidget m_shop9Button;
    public ButtonWidget m_shop10Button;
    public ButtonWidget m_shop11Button;
    public ButtonWidget m_shop12Button;
    public ButtonWidget m_shop13Button;

    public InventorySlotWidget m_soldSlot;
    public InventorySlotWidget m_soldSlot2;

    public ButtonWidget m_addcountButton;

    public ButtonWidget m_removecountButton;

    public SliderWidget m_countSlider;

    public int Count = 1;

    public int ItemValue = 0;

    public IInventory m_inventory;
    public SubsystemGameInfo m_subsystemGameInfo;
    public SubsystemTime subsystemTime;
    //下面是新人物状态按钮系统
    public ShopWidget(ComponentShop componentShop, ComponentPlayer componentPlayer, SubsystemNK subsystemHL)
    {
        m_componentPlayer = componentPlayer;
        m_subsystemHL = subsystemHL;
        m_componentShop = componentShop;
        m_inventory = m_componentPlayer.ComponentMiner.Inventory;
        XElement node = ContentManager.Get<XElement>("Widgets/nk");
        LoadContents(this, node);
        m_soldSlot = Children.Find<InventorySlotWidget>("SoldSlot");
        m_soldSlot2 = Children.Find<InventorySlotWidget>("SoldSlot2");
        m_shopButton = Children.Find<ButtonWidget>("ShopButton");
        m_shop1Button = Children.Find<ButtonWidget>("Shop1Button");
        m_shop2Button = Children.Find<ButtonWidget>("Shop2Button");
        m_shop3Button = Children.Find<ButtonWidget>("Shop3Button");
        m_shop4Button = Children.Find<ButtonWidget>("Shop4Button");
        m_shop5Button = Children.Find<ButtonWidget>("Shop5Button");
        m_shop6Button = Children.Find<ButtonWidget>("Shop6Button");
        m_shop7Button = Children.Find<ButtonWidget>("Shop7Button");
        m_shop8Button = Children.Find<ButtonWidget>("Shop8Button");
        m_shop9Button = Children.Find<ButtonWidget>("Shop9Button");
        m_shop10Button = Children.Find<ButtonWidget>("Shop10Button");
        m_shop11Button = Children.Find<ButtonWidget>("Shop11Button");
        m_shop12Button = Children.Find<ButtonWidget>("Shop12Button");
        m_shop13Button = Children.Find<ButtonWidget>("Shop13Button");
        m_countSlider = Children.Find<SliderWidget>("CountSlider");
        m_addcountButton = Children.Find<ButtonWidget>("AddCountButton");
        m_removecountButton = Children.Find<ButtonWidget>("RemoveCountButton");
        m_countSlider.MinValue = 1f;
        m_countSlider.MaxValue = 40f;
        m_countSlider.Granularity = 1f;
        m_countSlider.Value = Count;

    }

    public override void Update()
    {
        int activeBlockValue = m_componentPlayer.ComponentMiner.ActiveBlockValue;
        int num = Terrain.ExtractContents(activeBlockValue);
        int activeBlockValue2 = m_componentPlayer.ComponentMiner.Inventory.GetSlotCount(activeBlockValue);
        int num2 = Terrain.ExtractContents(activeBlockValue2);
        float Money = m_subsystemHL.Money;
        IInventory inventory = m_componentPlayer.ComponentMiner.Inventory;

        Count = MathUtils.Clamp((int)m_countSlider.Value, 1, 40);
        m_addcountButton.IsEnabled = (m_countSlider.Value < 40);
        m_removecountButton.IsEnabled = (m_countSlider.Value > 1);
        int Count2 = Count;
        if (m_removecountButton.IsClicked)
        {
            m_countSlider.Value--;
        }
        if (m_addcountButton.IsClicked)
        {
            m_countSlider.Value++;
        }
        m_countSlider.Text = "数量:" + Count + "\n" + "价值:" + m_subsystemHL.Money;

        if (m_shopButton.IsClicked)
        {
            if (m_subsystemHL.Money >= Count2)
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 177, (1 + Count2));
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (1 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

        if (m_shop1Button.IsClicked)
        {
            if (m_subsystemHL.Money >= (Count2 * 10))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 207, Count2);
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (10 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

        if (m_shop2Button.IsClicked)
        {
            if (m_subsystemHL.Money >= (Count2 * 10))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 42, Count2);
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (10 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

        if (m_shop3Button.IsClicked)
        {
            if (m_subsystemHL.Money >= (Count2 * 100))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 40, Count2);
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (100 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

        if (m_shop4Button.IsClicked)
        {
            if (m_subsystemHL.Money >= (Count2 * 200))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 111, Count2);
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (200 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

        if (m_shop5Button.IsClicked)
        {
            if (m_subsystemHL.Money >= (Count2 * 1000))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("交易成功", Color.White, blinking: true, playNotificationSound: false);
                m_componentShop.AddSlotItems(m_componentShop.SoldSlotIndex2, 583, Count2);
                m_componentShop.RemoveSlotItems(m_componentShop.SoldSlotIndex, (1000 * Count2));
            }
            else
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("价值不足或交易栏有物品", Color.White, blinking: true, playNotificationSound: false);
            }
        }

    }
}
